package uni.call.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.database.Cursor;


public class DailerActivity extends Activity
{
	//String To Hold peer Text View
	public static String Peer = "";

	public static String MyBalance = "";
	public static String RegStatus = "";
	
	final int PICK_CONTACT = 1001;
	
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dailer);
        	
        ImageButton button_1 = (ImageButton)findViewById(R.id.Number1);
        button_1.setOnClickListener(Number1Listener);
        
        ImageButton button_2 = (ImageButton)findViewById(R.id.Number2);
        button_2.setOnClickListener(Number2Listener);
        
        ImageButton button_3 = (ImageButton)findViewById(R.id.Number3);
        button_3.setOnClickListener(Number3Listener);
        
        ImageButton button_4 = (ImageButton)findViewById(R.id.Number4);
        button_4.setOnClickListener(Number4Listener);
        
        ImageButton button_5 = (ImageButton)findViewById(R.id.Number5);
        button_5.setOnClickListener(Number5Listener);
        
        ImageButton button_6 = (ImageButton)findViewById(R.id.Number6);
        button_6.setOnClickListener(Number6Listener);
        
        ImageButton button_7 = (ImageButton)findViewById(R.id.Number7);
        button_7.setOnClickListener(Number7Listener);
        
        ImageButton button_8 = (ImageButton)findViewById(R.id.Number8);
        button_8.setOnClickListener(Number8Listener);
        
        ImageButton button_9 = (ImageButton)findViewById(R.id.Number9);
        button_9.setOnClickListener(Number9Listener);
        
        ImageButton button_0 = (ImageButton)findViewById(R.id.Number0);
        button_0.setOnClickListener(Number0Listener);
        
        ImageButton button_star = (ImageButton)findViewById(R.id.NumberStar);
        button_star.setOnClickListener(NumberStarListener);
        
        ImageButton button_pound = (ImageButton)findViewById(R.id.NumberPound);
        button_pound.setOnClickListener(NumberPoundListener);
        
        ImageButton button_backspace = (ImageButton)findViewById(R.id.Backspace);
        button_backspace.setOnClickListener(BackspaceListener);
        button_backspace.setOnLongClickListener(LongBackspaceListener);
        
        ImageButton button_add = (ImageButton)findViewById(R.id.AddToContacts);
        button_add.setOnClickListener(AdderListener);
        
        ImageButton button_call = (ImageButton)findViewById(R.id.Call);
        button_call.setOnClickListener(CallListener);

    }
    
    private OnClickListener Number1Listener = new OnClickListener()
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.one);
        		DTMFSound.start();
        		
                break;
        	}

        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "1";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number2Listener = new OnClickListener()
    {
        public void onClick(View v)
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.two);
        		DTMFSound.start();
        		
                break;
        	}
   
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 	
        	Peer = Peer + "2";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number3Listener = new OnClickListener()
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.three);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "3";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number4Listener = new OnClickListener() 
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.four);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "4";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number5Listener = new OnClickListener() 
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.five);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "5";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number6Listener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.six);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "6";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number7Listener = new OnClickListener() 
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.seven);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "7";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number8Listener = new OnClickListener() 
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.eight);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "8";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number9Listener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.nine);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "9";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener Number0Listener = new OnClickListener()
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.zero);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "0";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener NumberStarListener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.star);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "*";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener NumberPoundListener = new OnClickListener() 
    {
        public void onClick(View v) 
        {
        	AudioManager MyAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        	switch (MyAudioManager.getRingerMode())
        	{
        	case AudioManager.RINGER_MODE_NORMAL:
        		MediaPlayer DTMFSound = MediaPlayer.create(getApplicationContext(), R.raw.pound);
        		DTMFSound.start();
        		
                break;
        	}
        	
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = Peer + "#";
        	CallNumberEditTest.setText(Peer);
        }
    };
    
    private OnClickListener BackspaceListener = new OnClickListener() 
    {
        public void onClick(View v)
        {
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	if (Peer.length() > 0) 
            {
                int strLen = Peer.length();
                int lastIdx = strLen - 1;
                Peer = Peer.substring(0, lastIdx);
            	CallNumberEditTest.setText(Peer);
            }
            else
            {

            }
        }
    };
    
    private OnLongClickListener LongBackspaceListener = new OnLongClickListener() 
    {
    	public boolean onLongClick(View v) 
    	{
        	EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
        	Peer = "";
        	CallNumberEditTest.setText("");
        	return true;
        }
    };
    
    private OnClickListener AdderListener = new OnClickListener()
    {
        public void onClick(View v) 
        {
        	final String[] Options = new String[2];
        	Options[0] = getString(R.string.Add_Contact);
        	Options[1] = getString(R.string.Edit_Contact);
        	AlertDialog.Builder alertDialog  = new AlertDialog.Builder(v.getContext());
        	alertDialog.setTitle(getString(R.string.Chose_Contact_Option));
        	alertDialog.setItems(Options, new DialogInterface.OnClickListener()
        	{
			    public void onClick(DialogInterface dialog, int item)
			    {
			    	if(item == 0)
			    	{
			    		EditText CallNumberEditTest = (EditText)findViewById(R.id.peer); 
			        	Intent intent = new Intent(Intent.ACTION_INSERT);
			        	intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
			        	intent.putExtra(ContactsContract.Intents.Insert.PHONE, CallNumberEditTest.getText());
			        	startActivity(intent);
			    	}
			    	if(item == 1)
			    	{

			    		Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
			    	    startActivityForResult(intent, PICK_CONTACT);
			    	}
			    }
			});
        	alertDialog.show();

        }
    };
    
    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);
        switch(reqCode)
        {
           case (PICK_CONTACT):
             if (resultCode == Activity.RESULT_OK)
             {
                 Uri contactData = data.getData();
                 Cursor c = managedQuery(contactData, null, null, null, null);
     
                 if (c.moveToFirst())
                 {
                	 EditText CallNumberEditTest = (EditText)findViewById(R.id.peer);
                     String Id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                     Intent i;
                     i = new Intent(Intent.ACTION_EDIT);
                     i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + Id));
                     i.putExtra( ContactsContract.Intents.Insert.PHONE, CallNumberEditTest.getText());
                     startActivity(i);
                 }
             }
        }
    }
    private OnClickListener CallListener = new OnClickListener()
    {
        public void onClick(View v)
        {
        	if (Peer.length() > 0) 
            {
            	Intent OutgoingIntent = new Intent(getApplicationContext(), OutGoingCall.class);
            	OutgoingIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            	OutgoingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            	startActivity(OutgoingIntent);
            }
            else
            {
            	Toast.makeText(DailerActivity.this, getString(R.string.No_Number), Toast.LENGTH_SHORT).show();
            }
        }
    };
}
